<template>
  <div class="filters">
    <el-row
      type="flex"
      class="filters-top"
      justify="space-between"
      align="middle"
    >
      <el-col :span="8">
        单程： {{ fdata.info.departCity }} - {{ fdata.info.destCity }} /
        {{ fdata.info.departDate }}
      </el-col>
      <el-col :span="4">
        <el-select
          size="mini"
          v-model="airport"
          placeholder="起飞机场"
          @change="linkageFilter"
        >
          <el-option
            :label="item"
            :value="item"
            v-for="(item, index) in fdata.options.airport"
            :key="index"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select
          size="mini"
          v-model="flightTimes"
          placeholder="起飞时间"
          @change="linkageFilter"
        >
          <el-option
            :label="`${item.from}:00 - ${item.to}:00`"
            :value="`${item.from},${item.to}`"
            v-for="(item, index) in fdata.options.flightTimes"
            :key="index"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select
          size="mini"
          v-model="company"
          placeholder="航空公司"
          @change="linkageFilter"
        >
          <el-option
            :label="item"
            :value="item"
            v-for="(item, index) in fdata.options.company"
            :key="index"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select
          size="mini"
          v-model="airSize"
          placeholder="机型"
          @change="linkageFilter"
        >
          <el-option
            :label="item.name"
            :value="item.value"
            v-for="(item, index) in airportSizes"
            :key="index"
          >
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="filter-cancel">
      筛选：
      <el-button
        type="primary"
        round
        plain
        size="mini"
        @click="handleFiltersCancel"
      >
        撤销
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: ["fdata"], // info  options
  //   props: {
  //     fdata: {
  //       type: Object,
  //       default: () => {
  //         // 默认的设置，如果有父传子情况下，不会执行
  //         return {};
  //       },
  //     },
  //   },
  data() {
    return {
      airport: "", // 机场
      flightTimes: "", // 出发时间
      company: "", // 航空公司
      airSize: "", // 机型大小
      airportSizes: [
        { name: "大", value: "L" },
        { name: "中", value: "M" },
        { name: "小", value: "S" },
      ],
    };
  },
  methods: {
    linkageFilter() {
      console.log(this.airport, this.flightTimes, this.company, this.airSize);
      // 以后对数据进行操作的时候，需要考虑是否需要保留一个副本，如果数据后期还有重复使用的可能，那么最好将数据复制一份进行操作
      let newList = [...this.fdata.flights];
      // 是否需要过滤机场
      if (this.airport) {
        newList = newList.filter((v) => {
          return v.org_airport_name == this.airport;
        });
      }
      if (this.flightTimes) {
        let [form, to] = this.flightTimes.split(","); //[6,12]
        newList = newList.filter((v) => {
          let start = +v.dep_time.split(":")[0]; // ["07","55"] >> 7
          return start >= form && start < to;
        });
      }
      if (this.company) {
        newList = newList.filter((v) => {
          return v.airline_name == this.company;
        });
      }
      if (this.airSize) {
        newList = newList.filter((v) => {
          return v.plane_size == this.airSize;
        });
      }
      this.$emit("filterData", newList);
    },
    // 选择机场时候触发
    handleAirport(value) {
      let newList = this.fdata.flights.filter((v) => {
        return v.org_airport_name == value;
      });
      this.$emit("filterData", newList);
    },

    // 选择出发时间时候触发
    handleFlightTimes(value) {
      // 6,12
      let arr = value.split(","); // [6,12]
      let newList = this.fdata.flights.filter((v) => {
        let start = +v.dep_time.split(":")[0]; // ["07","55"] >> 7
        return start >= arr[0] && start < arr[1];
      });
      this.$emit("filterData", newList);
    },

    // 选择航空公司时候触发
    handleCompany(value) {
      let newList = this.fdata.flights.filter((v) => {
        return v.airline_name == value;
      });
      // 实现子传父
      this.$emit("filterData", newList);
    },

    // 选择机型时候触发
    handleAirSize(value) {
      let newList = this.fdata.flights.filter((v) => {
        return v.plane_size == value;
      });
      // 实现子传父
      this.$emit("filterData", newList);
    },

    // 撤销条件时候触发
    handleFiltersCancel() {
      this.airport = ""; // 机场
      this.flightTimes = ""; // 出发时间
      this.company = ""; // 航空公司
      this.airSize = ""; // 机型大小
      // 还原数据
      this.$emit("filterData", this.fdata.flights);
    },
  },
};
</script>

<style scoped lang="less">
.filters {
  margin-bottom: 20px;
}

.filters-top {
  > div {
    /deep/ .el-select {
      margin-left: 10px;
    }
  }
}

.filter-cancel {
  margin-top: 10px;
}
</style>